package com.whh.dota2helper;

import java.text.DecimalFormat;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.whh.dota2helper.model.Ability;
import com.whh.dota2helper.model.Hero;
import com.whh.dota2helper.service.HeroService;

/**
 * 英雄主窗口
 * @author WHH
 *
 */
public class HeroActivity extends Activity {

	private static final DecimalFormat df = new DecimalFormat("0.00");
	
	private HeroService heroService ;
	
	private TextView tv_namecn;
	private TextView tv_roles;
	private TextView tv_int;
	private TextView tv_agi;
	private TextView tv_str;
	private TextView tv_attack;
	private TextView tv_speed;
	private TextView tv_defense;
	private TextView tv_hp_1;
	private TextView tv_hp_15;
	private TextView tv_hp_25;
	private TextView tv_mana_1;
	private TextView tv_mana_15;
	private TextView tv_mana_25;
	private TextView tv_damage_1;
	private TextView tv_damage_15;
	private TextView tv_damage_25;
	private TextView tv_armor_1;
	private TextView tv_armor_15;
	private TextView tv_armor_25;
	private TextView tv_sightrange;
	private TextView tv_attackrange;
	private TextView tv_missilespeed;
	private ImageView iv_header;
	private LinearLayout lv_abilities;
	
	private List<Ability> abilities;
	private Hero hero;
	
	//http://www.linuxidc.com/linux/2011-08/40980.htm
	//http://bbs.csdn.net/topics/350185089
	//http://www.dota2wiki.com/wiki/Earthshaker
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_hero);
		heroService = new HeroService(HeroActivity.this);
		
		init();
		initListener();
	}
	
	private void init(){
		long heroId = this.getIntent().getLongExtra("id", 91);
		//获取数据
		hero = heroService.getHero(heroId);
		abilities = hero.getAbilities();
		
		//读取控件
		tv_namecn = (TextView)this.findViewById(R.id.tv_title);
		tv_roles = (TextView)this.findViewById(R.id.tv_roles);
		tv_int = (TextView)this.findViewById(R.id.tv_int);
		tv_agi = (TextView)this.findViewById(R.id.tv_agi);
		tv_str = (TextView)this.findViewById(R.id.tv_str);
		tv_attack = (TextView)this.findViewById(R.id.tv_attack);
		tv_speed = (TextView)this.findViewById(R.id.tv_speed);
		tv_defense = (TextView)this.findViewById(R.id.tv_defense);
		tv_hp_1 = (TextView)this.findViewById(R.id.tv_hp_1);
		tv_hp_15 = (TextView)this.findViewById(R.id.tv_hp_15);
		tv_hp_25 = (TextView)this.findViewById(R.id.tv_hp_25);
		tv_mana_1 = (TextView)this.findViewById(R.id.tv_mana_1);
		tv_mana_15 = (TextView)this.findViewById(R.id.tv_mana_15);
		tv_mana_25 = (TextView)this.findViewById(R.id.tv_mana_25);
		tv_damage_1 = (TextView)this.findViewById(R.id.tv_damage_1);
		tv_damage_15 = (TextView)this.findViewById(R.id.tv_damage_15);
		tv_damage_25 = (TextView)this.findViewById(R.id.tv_damage_25);
		tv_armor_1 = (TextView)this.findViewById(R.id.tv_armor_1);
		tv_armor_15 = (TextView)this.findViewById(R.id.tv_armor_15);
		tv_armor_25 = (TextView)this.findViewById(R.id.tv_armor_25);
		tv_sightrange = (TextView)this.findViewById(R.id.tv_sightrange);
		tv_attackrange = (TextView)this.findViewById(R.id.tv_attackrange);
		tv_missilespeed = (TextView)this.findViewById(R.id.tv_missilespeed);
		iv_header = (ImageView)this.findViewById(R.id.iv_header);
		lv_abilities = (LinearLayout)this.findViewById(R.id.lv_abilities);
		
		//设置属性
		tv_namecn.setText(hero.getNameCn().split("\\|")[0]);
		tv_roles.setText(hero.getRoles());
		tv_int.setText(hero.getIntelligence()+" + "+df.format(hero.getIntelligenceIncrease()));
		tv_agi.setText(hero.getAgility()+" + "+df.format(hero.getAgilityIncrease()));
		tv_str.setText(hero.getStrength()+" + "+df.format(hero.getStrengthIncrease()));
		tv_attack.setText(hero.getAttackMin()+" - "+hero.getAttackMax());
		tv_speed.setText(hero.getMoveSpeed().toString());
		tv_defense.setText(hero.getDefense().toString());
		tv_sightrange.setText(hero.getSightRangeDay()+" / "+hero.getSightRangeNight());
		tv_attackrange.setText(hero.getAttackRange().toString());
		tv_missilespeed.setText(hero.getMissileSpeed().equals(0)?"N/A":hero.getMissileSpeed().toString());
		tv_hp_1.setText(hero.getHp().toString());
		tv_mana_1.setText(hero.getMana().toString());
		tv_damage_1.setText(hero.getDamageMin()+" - "+hero.getDamageMax());
		tv_armor_1.setText(df.format(hero.getArmor()));
		hero.setLevel(15);
		tv_hp_15.setText(hero.getHp().toString());
		tv_mana_15.setText(hero.getMana().toString());
		tv_damage_15.setText(hero.getDamageMin()+" - "+hero.getDamageMax());
		tv_armor_15.setText(df.format(hero.getArmor()));
		hero.setLevel(25);
		tv_hp_25.setText(hero.getHp().toString());
		tv_mana_25.setText(hero.getMana().toString());
		tv_damage_25.setText(hero.getDamageMin()+" - "+hero.getDamageMax());
		tv_armor_25.setText(df.format(hero.getArmor()));
		//设置头像
		int headerId = this.getResources().getIdentifier(hero.getHeaderVert(),"drawable", getPackageName());
		iv_header.setImageResource(headerId);
		//设置技能
		for(int i=0;i<abilities.size();i++){
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
			lp.setMargins(0, 10, 0, 0);  
			View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item_ability, null);
			view.setLayoutParams(lp); 
			final Ability ability = abilities.get(i);
			TextView tv_ability_name = (TextView)view.findViewById(R.id.tv_ability_name);
			TextView tv_ability_desc = (TextView)view.findViewById(R.id.tv_ability_desc);
			tv_ability_name.setText(ability.getName());
			tv_ability_desc.setText(ability.getDesc());
			ImageView iv = (ImageView)view.findViewById(R.id.iv_ability);
			int id = view.getResources().getIdentifier(ability.getImg(),"drawable", getPackageName());
			iv.setImageResource(id);
			//技能点击
			view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(HeroActivity.this,HeroAbilityActivity.class);
					intent.putExtra("id", ability.getId());
					startActivity(intent);
				}
			});
			lv_abilities.addView(view);
		}
	}
	
	private void initListener(){
		//头像点击
		iv_header.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(HeroActivity.this,HeroBioActivity.class);
				intent.putExtra("id", hero.getId());
				startActivity(intent);
			}
		});
	}
	
	/**
	 * 动态设置ListView的高度
	 * Item中如果有TextView,则只计算一行的高度，所以需要自己判断一段文字有几行，再计算高度加进去
	 * 暂时无解决办法
	 * @param listView
	 */
	public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight = totalHeight + listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        //if without this statement,the listview will be a little short
        params.height = params.height + 5;
        
        listView.setLayoutParams(params);
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
